﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace DumpCSV
{
    public partial class Form1 : Form
    {
        private string srcPath;
        private string destPath;
        private string types;

        public Form1 () {
            InitializeComponent();
        }


        private void SrcSelectBtn_Click (object sender, EventArgs e) {
            FolderBrowserDialog fb = new FolderBrowserDialog();
            if (fb.ShowDialog() == DialogResult.OK) {
                SrcPathBox.Text = fb.SelectedPath;
                if (DestPathBox.Text.Length == 0)
                    DestPathBox.Text = fb.SelectedPath + "_backup";
            }
        }

        private void DestSelectBtn_Click (object sender, EventArgs e) {
            FolderBrowserDialog fb = new FolderBrowserDialog();
            if (fb.ShowDialog() == DialogResult.OK)
                DestPathBox.Text = fb.SelectedPath;
        }

        private void DumpBtn_Click (object sender, EventArgs e) {
            srcPath = SrcPathBox.Text;
            destPath = DestPathBox.Text;
            types = TypesBox.Text;
            CopyDir(srcPath);
            MessageBox.Show("备份完成！");
        }

        private void CopyDir (string path) {
            if (Directory.Exists(path)) {
                string dest = destPath + GetRelativePath(srcPath, path);
                string[] files = Directory.GetFiles(path);
                foreach (string file in files) {
                    if (!TypeInFilter(file, types)) continue;
                    CreateDir(dest);
                    string destFile = dest + '\\' + GetFileName(file);
                    File.Copy(file, destFile, true);
                }

                string[] dirs = Directory.GetDirectories(path);
                foreach (string dir in dirs) CopyDir(dir);
            }
        }
        private string GetRelativePath(string basePath, string path) {
            if (path.IndexOf(basePath) != 0) return null;
            int cnt = path.Length - basePath.Length;
            return path.Substring(basePath.Length, cnt);
        }
        private void CreateDir(string path) {
            if (path == null || Directory.Exists(path)) return;
            CreateDir(GetParentDir(path));
            Directory.CreateDirectory(path);
        }
        private string GetParentDir(string path) {
            int idx = path.LastIndexOf('\\');
            if (idx < 0) idx = path.LastIndexOf('/');
            if (idx < 0) return null;
            return path.Substring(0, idx);
        }


        private bool TypeInFilter(string file, string filter) {
            string[] types = filter.Split(',');
            string ft = GetType(file);
            if (ft == null) return false;

            foreach (string type in types) {
                if (type == ft) return true;
            }
            return false;
        }
        private string GetType(string path) {
            int idx = path.LastIndexOf('.');
            if (idx < 0) return null;
            return path.Substring(idx + 1, path.Length - (idx + 1));
        }
        private string GetFileName(string path) {
            int idx = path.LastIndexOf('\\');
            if (idx < 0) idx = path.LastIndexOf('/');
            if (idx < 0) return null;
            return path.Substring(idx + 1, path.Length - (idx + 1));
        }

    }
}
